import requests
from parsel import Selector


class CollectionCatalogue:

    def __init__(self, url):
        self.url = url
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36'
        }
        self.chapter_url_list = []

    def get_catalogue(self):
        try:
            resp = requests.get(url=self.url, headers=self.headers)
            self.parse_chapter(resp.text)
        except:
            pass

    def parse_chapter(self, html):
        selector = Selector(text=html)
        url_list = selector.css('.chapter.clearfix a::attr(href)').getall()
        for url in url_list:
            url = 'https://www.tadu.com' + url.strip()
            self.chapter_url_list.append(url)

    def run(self):
        self.get_catalogue()
        return self.chapter_url_list


if __name__ == '__main__':
    catalogue_url = 'https://www.tadu.com/book/catalogue/1011591'
    spider = CollectionCatalogue(catalogue_url)
    print(spider.run())
